package com.example.demo31;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class DialogActivity2 extends AppCompatActivity {


private String[] titlesize= new String[]{"小号","中号","大号"};
private int[]  titlesizeArr  = new int[]{10,20,30};
private TextView textView;

private String[]  titlechoices = new String[]{"篮球","排球","足球","乒乓球"};

    private boolean[] tileChoiceArray = new boolean[]{false,false,false,false}; // 默认全为 false

private int index;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); // 必须第一行
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_dialog); // 必须第二行

        // 正确初始化成员变量 textView
        textView = findViewById(R.id.textSize);

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }

    public void showNoneDialong(View view)
    {
        AlertDialog alertDialog =  new AlertDialog.Builder(this)
                .setTitle("温馨提示")
                .setMessage("确定要退出吗？")
                .setNegativeButton("取消",null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                       // finish();// 退出当前Activity
                        // 可选：完全结束应用进程（根据需求选择）
                        // System.exit(0);
                    }
                })
                .create();
        alertDialog.show();
    }
    public void showDialog(View view)
    {
        new AlertDialog.Builder(this)
                .setTitle("请选择字体大小")
                .setSingleChoiceItems(titlesize, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        index = i;
                    }
                }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        textView.setTextSize(titlesizeArr[index]);

                    }
                }).setNegativeButton("取消",null).create().show();

    }
    public void MulitDialog(View view)
    {
        new AlertDialog.Builder(this)
                .setTitle("请选择你的爱好")
                .setMultiChoiceItems(titlechoices, tileChoiceArray, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i, boolean isChecked) {
                        tileChoiceArray[i] = isChecked;


                    }
                }).setNegativeButton("取消",null).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        StringBuilder stringBuilder = new StringBuilder();

                        // 修复循环条件
                        for(int j = 0; j < tileChoiceArray.length; j++) {
                            if(tileChoiceArray[j]) {
                                // 添加非空判断避免 titlechoices 越界
                                if(titlechoices != null && j < titlechoices.length) {
                                    stringBuilder.append(titlechoices[j]).append("\n"); // 换行分隔
                                }
                            }
                        }

                        String result = stringBuilder.length() > 0 ?
                                stringBuilder.toString() : "未选择任何爱好";


                        Toast.makeText(DialogActivity2.this,result,Toast.LENGTH_SHORT).show();
                    }
                }).create().show();
    }
}